<%--
  Created by IntelliJ IDEA.
  User: hnjy
  Date: 2025/2/25
  Time: 13:56
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="/WEB-INF/jsp/common/headHome.jsp" %>
<script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/jquery-1.8.3.min.js"></script>
<html>
<head>

    <title>宠物论坛</title>
    <style>
        /* 全局样式 */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
            position: relative;
        }

        /* 导航栏样式 */
        .navbar {
            background-color: #a1d1c5;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 20px;
        }

        .nav-links {
            list-style-type: none;
            margin: 0;
            padding: 0;
            display: flex;
        }

        .nav-links li {
                margin-right: 20px;
        }

        .nav-links a {
            color: white;
            text-decoration: none;
        }

        .user-icon img {
            border-radius: 50%;
        }

        .logout a {
            color: white;
            text-decoration: none;
        }

        /* 论坛标题样式 */
        .forum-title {
            text-align: center;
            margin: 50px 0;
        }

        .forum-title h1 {
            font-size: 36px;
            margin: 0;
        }

        /* 帖子列表样式 */
        .post-list {
            width: 400px;
            margin: 0 auto;
        }

        .post-item {
            background-color: #fff;
            border: 1px solid #ccc;
            padding: 10px;
            margin-bottom: 10px;
            cursor: pointer;
        }

        /* 发布帖子按钮样式 */
        .post-button {

        }

        .post-button button {
            background-color: #42b983;
            color: white;
            border: none;
            padding: 8px 15px;
            cursor: pointer;
        }

        /* 分页导航样式 */
        .pagination {
            text-align: center;
            margin-top: 30px;
        }

        .page-link {
            display: inline-block;
            padding: 5px 10px;
            margin: 0 5px;
            text-decoration: none;
            color: #333;
            border: 1px solid #ccc;
        }

        .active {
            background-color: #42b983;
            color: white;
        }

        .prev,
        .next {
            cursor: pointer;
        }

        /* 背景图案样式 */
        .bg-pattern {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: none;
        }

        .bg-pattern img {
            position: absolute;
        }

        .pet-info {
            margin-bottom: 20px;
        }

        .comment-input {
            margin-top: 20px;
        }

        .comment-input textarea {
            width: 100%;
            height: 100px;
        }

        .comment-item {
            border: 1px solid #ccc;
            padding: 10px;
            margin-bottom: 10px;
        }

        .comment-item img {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            vertical-align: middle;
        }

        .comment-item.info {
            display: inline-block;
            margin-left: 10px;
        }

        .comment-item.username {
            font-weight: bold;
        }

        .comment-item.time {
            color: #999;
        }

        .reply-btn {
            margin-top: 5px;
        }

        .reply-list {
            margin-left: 30px;
        }

        .modal {
            display: none;
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.4);
        }

        /* 弹窗内容样式 */
        .modal-content {
            background-color: #fefefe;
            margin: 15% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            max-width: 500px;
            border-radius: 5px;
            box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
        }

        /* 关闭按钮样式 */
        .close {
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: #000;
            text-decoration: none;
            cursor: pointer;
        }

        .btn {
            background-color: #337AB7;
            color: white;
            border-radius: 3px;
            border: unset;
            width: 60px;
            height: 40px;
        }
    </style>
</head>
<body>
<div style="display: flex;flex-direction: column;justify-content: center;align-items: center;width: 60%;margin: 0 auto">

    <!-- 论坛标题 -->
    <div class="forum-title">
        <%--    <h1>论坛</h1>--%>
        <!-- 发布帖子按钮 -->
        <div class="post-button">
            <button onclick="showDiv()">发布帖子</button>
        </div>

    </div>

    <!-- 帖子列表 -->
    <div class="post-list">
        <c:forEach items="${list}" var="forum">
            <a style="text-decoration: none" href="${pageContext.request.contextPath}/sys/forum/detail?id=${forum.id}">
            <div class="post-item" style="background-color: #E4E4E4">
                <span>${forum.title}</span>
            </div></a>
        </c:forEach>
    </div>


    <!-- 分页导航 -->
    <div class="pagination">
        <a href="${pageContext.request.contextPath}/sys/forum/list?pageIndex=1" class="page-link">首页</a>
        <c:if test="${currentPageNo>1}">
            <a href="${pageContext.request.contextPath}/sys/forum/list?pageIndex=${currentPageNo-1}"
               class="page-link prev">&lt;&lt;</a>
        </c:if>

        <c:forEach begin="1" end="${totalPageCount}" var="pageIndex">
            <a href="${pageContext.request.contextPath}/sys/forum/list?pageIndex=${pageIndex}" class="page-link    <c:if test="${currentPageNo==pageIndex}">active</c:if>" >${pageIndex}</a>
        </c:forEach>
        <c:if test="${currentPageNo<totalPageCount}">
            <a href="${pageContext.request.contextPath}/sys/forum/list?pageIndex=${currentPageNo+1}"
               class="page-link next">&gt;&gt;</a>
        </c:if>
        <a href="${pageContext.request.contextPath}/sys/forum/list?pageIndex=${totalPageCount}" class="page-link">末页</a>
    </div>
</div>


<div id="modal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <div class="comment-input">
            <form action="${pageContext.request.contextPath}/forum/add" method="post" id="form">
                <input id="userId" type="hidden" name="parentCommentId">
                标题：<input name="title" id="title"/>
                <textarea name="content" placeholder="来说几句吧" id="content" style="margin-top: 10px"></textarea>
                <input type="hidden" name="petId" value="${petId}"> <!-- 这里假设宠物ID为1，实际应动态获取 -->
                <div class="btn"
                     style="background-color:#337AB7;color: white;line-height: 30px;height: 30px; width: 60px;text-align: center "
                     onclick="publish()">发布
                </div>
            </form>
        </div>
    </div>
</div>

</body>
</html>
<script>
    function publish() {
        var content = $("#content").val();
        var title = $("#title").val();

        $.post("${pageContext.request.contextPath}/sys/forum/add", "content=" + content + "&title=" + title, function (data) {
            if (data == true) {
                alert("发布成功");
                location = "${pageContext.request.contextPath}/sys/forum/list"
            } else {
                alert("发布失败");
            }

        })
        // document.getElementById("form").submit();
    }

    function showDiv() {
        // 获取按钮、弹窗和关闭按钮元素

        const modal = document.getElementById('modal');
        const closeBtn = document.querySelector('.close');


        modal.style.display = 'block';


        // 点击关闭按钮关闭弹窗
        closeBtn.addEventListener('click', () => {
            modal.style.display = 'none';
        });

        // 点击弹窗外部关闭弹窗
        window.addEventListener('click', (event) => {
            if (event.target === modal) {
                modal.style.display = 'none';
            }
        });
    }
</script>
